package com.emep.changzhi.analyse.service.aqi;


import com.emep.changzhi.analyse.entity.aqi.AqiNumberOfDays;
import com.emep.changzhi.analyse.entity.aqi.DataAqiInfo;
import com.emep.changzhi.analyse.entity.basics.SysAqiStandardDayInfo;
import com.emep.changzhi.analyse.repository.aqi.AqiNumberOfDaysRepository;
import com.emep.changzhi.analyse.repository.aqi.DataAqiInfoRepository;
import com.emep.changzhi.analyse.repository.basics.SysAqiStandardDayRepository;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.*;

@Slf4j
@Data
@Service
@Transactional
public class AqiNumberOfDaysService {
    @Resource
    AqiNumberOfDaysRepository aqiNumberOfDaysRepository;
    @Resource
    SysAqiStandardDayRepository sysAqiStandardDayRepository;
    @Resource
    DataAqiInfoRepository dataAqiInfoRepository;
    
    /**
     * @author xy
     * @Description: 获取城市AQI的统计情况
     * @date 2018/4/2 11:41
     * @version V1.0
     */
    
    public Map<String,Object> findStandardAndNumberOfDay(){
        Map<String,Object> map = new HashMap<>();
        List<SysAqiStandardDayInfo> list_standard = sysAqiStandardDayRepository.findAll();
        for(int i=0;i<list_standard.size();i++){
            Map<String,Object> map_standard = new HashMap<>();
            map_standard.put("pm2_5max",list_standard.get(i).getPm25Max());
                map_standard.put("goalDay",list_standard.get(i).getExcellentMax());
                map_standard.put("maxDay",list_standard.get(i).getPollutionMax());
                List<AqiNumberOfDays> list_number = aqiNumberOfDaysRepository.findByYear(list_standard.get(i).getYear());
                int excellent_max = 0;
                int pollution_max = 0;
                double pm2_5 = 0;
                for(int j=0;j<list_number.size();j++){
                    excellent_max += Integer.parseInt(list_number.get(j).getExcellentMax());
                    pollution_max += Integer.parseInt(list_number.get(j).getPollutionMax());
                    pm2_5 += Double.parseDouble(list_number.get(j).getPm25Max());
                }
                double pm2_5ed = pm2_5/list_number.size();
                if(list_number.size()==0){
                    pm2_5ed = 0.0;

                }
                if(list_standard.get(i).getYear().equals("2020") || list_standard.get(i).getYear()== "2020"){
                    map.put(list_standard.get(i).getYear(),map_standard);
                    continue;
                }
                map_standard.put("fineDay",String.valueOf(excellent_max));
                map_standard.put("heavyDay",String.valueOf(pollution_max));
                map_standard.put("pm2_5ed",String.valueOf(pm2_5ed));
                map.put(list_standard.get(i).getYear(),map_standard);
            }
        return map;
    }
    /**
     * @author xy
     * @Description: 统计某一个国控站点在48小时之内6参数的折线图
     * @date 2018/4/2 11:41
     * @version V1.0
     */
    
    public Map<String,Object> findBysite(String area_name,String site_name){
        Map<String,Object> map_pm2_5 = new HashMap<>();
        Map<String,Object> map_pm10 = new HashMap<>();
        Map<String,Object> map_co = new HashMap<>();
        Map<String,Object> map_so2 = new HashMap<>();
        Map<String,Object> map_o3 = new HashMap<>();
        Map<String,Object> map_no2 = new HashMap<>();

        Map<String,Object> map_all = new HashMap<>();
        String date_end = DateTimeUtil.getDateTimeForString(new Date(),DateTimeUtil.DATE_DEFAULT_FORMAT)+" "+ DateTimeUtil.getNowHour()+":00:00";
        Date date = null;
        try {
            date = DateTimeUtil.stringToDate(date_end,DateTimeUtil.DATETIME_DEFAULT_FORMAT);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date_save = DateTimeUtil.mathHourOfDate(new Date(),-48);
        String save = DateTimeUtil.getDateTimeForString(date_save,"yyyy-MM-dd HH")+":00:00";
        Date date_save1 = null;
        try {
            date_save1= DateTimeUtil.stringToDate(save,DateTimeUtil.DATETIME_DEFAULT_FORMAT);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<DataAqiInfo> list = dataAqiInfoRepository.findByAreaIdAndPositionNameAndTimePointBetween(area_name,site_name,date_save1.getTime()+"",date.getTime()+"");
        List<String> list_Date=new ArrayList<>();
        try {
            list_Date = DateTimeUtil.getHourDateArrayString(save,DateTimeUtil.DATETIME_DEFAULT_FORMAT,date_end,DateTimeUtil.DATETIME_DEFAULT_FORMAT,1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for(int i = 0;i<list_Date.size();i++){
            list_Date.get(i);
            for(int j = 0;j<list.size();j++){
                String a = list_Date.get(i);
                Date date_before= null;
                try {
                    date_before = DateTimeUtil.stringToDate(a,DateTimeUtil.DATETIME_DEFAULT_FORMAT);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String date_before_a=date_before.getTime()+"";
                String as = list.get(j).getTimePoint().toString();
                if(date_before_a == as || date_before_a.equals(as)){
                    map_pm2_5.put(list_Date.get(i),list.get(j).getPm25().toString());
                    map_pm10.put(list_Date.get(i),list.get(j).getPm10().toString());
                    map_co.put(list_Date.get(i),list.get(j).getCo().toString());
                    map_o3.put(list_Date.get(i),list.get(j).getO3().toString());
                    map_no2.put(list_Date.get(i),list.get(j).getNo2().toString());
                    map_so2.put(list_Date.get(i),list.get(j).getSo2().toString());
                }
            }
            map_all.put("PM2_5" , map_pm2_5);
            map_all.put("PM10" , map_pm10);
            map_all.put("CO" , map_co);
            map_all.put("O3" , map_o3);
            map_all.put("NO2" , map_no2);
            map_all.put("SO2" , map_so2);
        }
        return map_all;
    }

    /**
     * 月污染天数
     * @return
     */
    public Map<String,Object> findapiByMonth(String areaCode) {
        AqiNumberOfDays aqiNumberOfDays = new AqiNumberOfDays();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        //String time=String.valueOf(year)+"-"+String.valueOf(month);
        String time=year + "-" + ( month<10 ? "0" + month : month);
        aqiNumberOfDays= aqiNumberOfDaysRepository.findByYearAndAreaCode(time,areaCode);
       /* for(int i = 0;i<aqiNumberOfDaysList.size();i++) {
            aqiNumberOfDays = aqiNumberOfDaysList.get(i);

        }*/
        Map<String,Object> map = new HashMap<>();
        map.put("PollutionMax",aqiNumberOfDays.getPollutionMax());
        map.put("Pm2.5Max",aqiNumberOfDays.getPm25Max());
        System.out.println(map);
        return map;
    }

    /**
     * 年污染天数
     * @return
     */
    public Map<String,Object>  findapiByYear(String areaCode) {
        AqiNumberOfDays aqiNumberOfDays = new AqiNumberOfDays();
        Calendar cal = Calendar.getInstance();
        String year = String.valueOf(cal.get(Calendar.YEAR));
        aqiNumberOfDays= aqiNumberOfDaysRepository.findByYearAndAreaCode(year,areaCode);
        /*for(int i = 0;i<aqiNumberOfDaysList.size();i++) {
            aqiNumberOfDays = aqiNumberOfDaysList.get(i);
        }*/
        Map<String,Object> map = new HashMap<>();
        map.put("PollutionMax",aqiNumberOfDays.getPollutionMax());
        map.put("Pm2.5Max",aqiNumberOfDays.getPm25Max());
        System.out.println(map);
        return map;
    }

}
